package com.history.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;

import com.history.common.Logit;
import com.history.domain.bean.Article;


/**
 * A data access object (DAO) providing persistence and search support for
 * Article entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see Article.metaweb.domain.bean.Article
 * @author MyEclipse Persistence Tools
 */

public class ArticleDAO extends BaseHelperDAO {
	private static final Logit log = Logit.getInstance(ArticleDAO.class);
	// property constants
	public static final String ID = "id";

	protected void initDao() {
		// do nothing
	}

	public void save(Article transientInstance) {
		log.debug("saving Article instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Article persistentInstance) {
		log.debug("deleting Article instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Article findById(java.lang.String id) {
		log.debug("getting Article instance with id: " + id);
		try {
			Article instance = (Article) getHibernateTemplate().get(
					"com.history.domain.bean.Article", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Article> findAll() {
		log.debug("finding all Article instances");
		try {
			String queryString = "from Article";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public void updateArticle(Article article) {
		try {
			Query query = getSession().getNamedQuery("article.updateArticle");
            query.setParameter("id", article.getId());
            query.setParameter("title", article.getTitle());
            query.setParameter("content", article.getContent());
            query.setParameter("sysKey", article.getSysKey());
            query.setParameter("updateDate", new Date());
            query.executeUpdate();
 
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}	
	
}